﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.XPath;
using System.Xml;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Security.Policy;

namespace FortuneHeaven.Web.Controls
{
    public partial class CustomNavigator : System.Web.UI.UserControl
    {
        public static XmlDocument doc;
        static CustomNavigator()
        {
            try
            {
                doc = new XmlDocument();
                HttpServerUtility server = HttpContext.Current.Server;
                string path = server.MapPath("~/Site.xml");
                doc.Load(path);
                string attr;
                XmlNodeList list = doc.SelectNodes("//Node");
                foreach (XmlNode tempnode in list)
                {
                    XmlElement node = tempnode as XmlElement;
                    attr = node.GetAttribute("url");
                    if (!string.IsNullOrEmpty(attr))
                    {
                        node.SetAttribute("url", attr.Trim().ToLower());
                    }
                }
            }
            catch
            {
            }
        }

        public bool CheckParam(string input)
        {
            if (!string.IsNullOrEmpty(input))
            {
                string[] content = input.Split(',');
                foreach (string k in content)
                {
                    string[] cur = k.Split('=');
                    if (Request[cur[0].Trim()] != cur[1])
                    {
                        return false;
                    }
                }
            }
            return true;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            StringBuilder builder = new StringBuilder();
            Stack<XmlElement> content = new Stack<XmlElement>();
            if (doc != null)
            {
                string current = Request.AppRelativeCurrentExecutionFilePath;
                XmlNodeList list = (XmlNodeList)doc.SelectNodes(string.Format("//Node[@url = '{0}']", current.ToLower()));
               
                XmlElement el = null;
                if (list != null && list.Count > 0)
                {
                    el = (XmlElement)list[0];
                    //foreach (XmlNode tempnode in list)
                    //{
                    // XmlElement node = tempnode as XmlElement;
                    // string attr = node.GetAttribute("param");
                    // if (CheckParam(attr))
                    // {
                    // el = (XmlElement)node;
                    // }
                    //}
                }
                if (el == null)
                {
                    return;
                }
                XmlElement pa = el;
                while (pa != null)
                {
                    content.Push(pa);
                    if (pa.ParentNode is System.Xml.XmlDocument)
                    {
                        break;
                    }
                    pa = (XmlElement)pa.ParentNode;
                }
                if (content.Count > 0)
                {
                  
                    while (content.Count != 1)
                    {
                        XmlElement node = content.Pop();
                        builder.Append(
                        string.Format("<a href='{0}'>{1}</a> > ", VirtualPathUtility.ToAbsolute(node.GetAttribute("url")), node.GetAttribute("description")));
                    }
                    //if (this.Page != null)
                    //{
                    // Type pagetype = this.Page.GetType();
                    // if (pagetype.GetProperty("SiteMapDes") != null)
                    // {
                    // pagetype.GetProperty("SiteMapDes").GetValue(this.Page, null);
                    // }
                    //}
                }
                Response.Write(builder.ToString());
            }
        }
    }
}


